﻿#include <stdlib.h>
#include "CMetalSlug.h"

#define BACKGROUND  "Map1.png"
#define MARCO		"Leg_Run1_31x20_18.png"
#define MARCO_LEG_JUMP_UP	"Leg_Jump_18_24_1.png"
#define MARCO_LEG_RUN		"Leg_Run1_31x20_18.png"
#define MARCO_LEG_STANDON	"Leg_StandOn_21x16_4_16.png"

#define GROUND_Y	600	//the ground
#define MAX_Y		260				//the max jump

#define RIGHT	0
#define LEFT	1

CMetalSlug::CMetalSlug(HINSTANCE hInstance, LPCSTR GameName, int iMode, bool bIsWindowed, int iFPS):
CGame(hInstance, GameName, iMode, bIsWindowed, iFPS)
{	
	m_bIsAction = false;
	m_BackGround = NULL;
	Tree = new CQuadTree(800, 600);
	Screen = new CScreen( 0, 0, 1363*(float) mi_ScreenWidth / 320, 289*(float) mi_ScreenWidth / 320);
}

CMetalSlug::~CMetalSlug()
{
	Tree = NULL;
}

//virtual methods
void CMetalSlug::LoadResource()
{
	m_BackGround = CGame::CreateSurfaceFromFile(BACKGROUND);

	HRESULT result = D3DXCreateSprite(mp_Direct3D->GetDirectDevice(), &m_SpriteHandler);

	mp_Direct3D->GetDirectDevice()->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &m_BackBuffer);
	
	Marco = new CMarco(m_SpriteHandler, (float) mi_ScreenWidth / 320);
	Rebel = new CRebel(m_SpriteHandler, (float) mi_ScreenWidth / 320, Tree);
	Tree->AddObj(Tree->m_qRoot, Rebel);
	InitRebel();
}

void CMetalSlug::ProcessInput(void)
{
	//LEFT ARROW press event
	//run to the left
	if (KEY_DOWN(DIK_LEFT))  
	{	
		if (Marco->mb_IsJump)
		{
			Marco->mf_vx = -0.5;
			return;
		}
		
		if (KEY_DOWN(DIK_SPACE))
		{
			Marco->JumpFar(LEFT);
		}
		else if (KEY_DOWN(DIK_Z))
		{
			Marco->Shot();			
		}
		else if (KEY_DOWN(DIK_X))
		{
			Marco->ThrowGrenade();
		}
		
		else if (KEY_DOWN(DIK_UP))
		{
			if (KEY_DOWN(DIK_Z))
			{
				Marco->ShotUp();
			}
		}

		if (!Marco->mb_IsJump)
		{
			Marco->Run(LEFT);
		}

		mdw_LastKeyPress = GetTickCount();
	}
	//RIGHT ARROW press event
	//run to the right
	else if (KEY_DOWN(DIK_RIGHT))
	{
		Screen->MoveCamera(1);
		//m_bIsAction = true;
		//Rebel->Crawl();
		if (Marco->mb_IsJump)
		{
			Marco->mf_vx= 0.5;
			return;
		}
		if (KEY_DOWN(DIK_SPACE))
		{
			Marco->JumpFar(RIGHT);
		}
		else if (KEY_DOWN(DIK_Z))
		{
			Marco->Shot();			
		}
		else if (KEY_DOWN(DIK_X))
		{
			Marco->ThrowGrenade();
		}
		
		else if (KEY_DOWN(DIK_UP))
		{
			if (KEY_DOWN(DIK_Z))
			{
				
				Marco->ShotUp();
			}
		}
		if (!Marco->mb_IsJump)
		{
			Marco->Run(RIGHT);
		}
		mdw_LastKeyPress = GetTickCount();
	}

	//DOWN ARROW press event
	//crouch move and shoting down
	else if (KEY_DOWN(DIK_DOWN))
	{
		if (KEY_DOWN(DIK_B))
		{
			Marco->CrouchMove(RIGHT);
		}
		else if (KEY_DOWN(DIK_V))
		{
			Marco->CrouchMove(LEFT);
		}
		else if (KEY_DOWN(DIK_Z))
		{
			Marco->CrouchShot();
		}
		else
		{
			Marco->Crouch();
		}
		mdw_LastKeyPress = GetTickCount();
	}

	//UP ARROW press event
	//shoting up to the air
	else if (KEY_DOWN(DIK_UP))
	{
		if (KEY_DOWN(DIK_Z))
		{
			Marco->ShotUp();
		}
	}

	//SPACE ARROW press event
	//jump up and jump far
	else if (KEY_DOWN(DIK_SPACE))
	{ 
		if (Marco->mb_IsJump)
			return;
		
		Marco->JumpUp();
		mdw_LastKeyPress = GetTickCount();
	}

	//Z ARROW press event
	//Shoting event
	else if (KEY_DOWN(DIK_Z))
	{
		if (Marco->mb_IsAction)
		{
			Marco->mb_ShotContinue = true;
			return;
		}
		Marco->Shot();
		
		mdw_LastKeyPress = GetTickCount();
	}

	//LEFT ARROW press event
	//Throw greanade event
	else if (KEY_DOWN(DIK_X))
	{
		Marco->ThrowGrenade();
		
		mdw_LastKeyPress = GetTickCount();
	}

	//nothing pressed
	//stand and relax event
	else
	{
		DWORD now = GetTickCount();

		if (Marco->mb_IsJump)
		{
			return;
		}
		else
		{
			if ( !(Marco->mb_IsRelax)&&(now - mdw_LastKeyPress)/1000 > 5)
			{
				Marco->Relax();
				mdw_LastKeyPress = now;
			}
			else
			{
				Marco->Stand();
			}
		}
	}
}

void CMetalSlug::UpdateFrame(void)
{
	vector<Object*> res;
	Marco->Update(mdw_DeltaTime);
	Tree->getObjInRect(Tree->m_qRoot, Screen->m_RCamera, res);
	//Tree->CheckCollision(Tree->m_qRoot, Screen,)
	/*char str[25];
	itoa(res.size(), str, 10);
	MessageBox(m_WndHandle, str , "Error", MB_OK);*/
}

void CMetalSlug::RenderFrame(void)
{
	UpdateFrame();
	//vector<Object*> result;
	//Tree->getAllObj(Tree->m_qRoot, result);
	//drawing backGround
	mp_Direct3D->GetDirectDevice()->StretchRect(m_BackGround,	//from
												NULL,			//which protion
												m_BackBuffer,	//to
												NULL,			//which protion
												D3DTEXF_POINT);	//filter which is decide how to stretch the source image to Destination Rect
	
	//lock the surface so that the sprite can be draw
	m_SpriteHandler->Begin(D3DXSPRITE_ALPHABLEND); 
	
	//set the sampler state that tell the device which technology will be use to render
	mp_Direct3D->GetDirectDevice()->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
	Marco->Render();
	Tree->DrawObj(Screen->m_RCamera);
	//unlock the surface for other processes to use
	m_SpriteHandler->End();
}

void CMetalSlug::InitRebel()
{
	/*Rebel = new CRebel(m_SpriteHandler, (float) mi_ScreenWidth / 320,);
	Rebel->Die3();
	m_Rebel.push_back(Rebel);

	Rebel = new CRebel(m_SpriteHandler, (float) mi_ScreenWidth / 320);
	Rebel->m_iX = 400;
	if(m_bIsAction == true)
		Rebel->Crawl();
	//Rebel->Talk();
	m_Rebel.push_back(Rebel);*/
	Rebel = new CRebel(m_SpriteHandler, (float) mi_ScreenWidth / 320, Tree);
	Rebel->m_iX = 500;
	Rebel->TalkFone();
	Tree->AddObj(Tree->m_qRoot, Rebel);
}
void CMetalSlug::SetupTree()
{
	//Tree->Setup(m_Rebel);
}
void CMetalSlug::OnKeyUp(int iKeyCode)
{
}

void CMetalSlug::OnKeyDown(int iKeyCode)
{	
}

